package com.hbm.usercenter.base;

import com.hbm.usercenter.http.MyCallBack;
import com.hbm.usercenter.http.NetClient;
import com.hbm.usercenter.utils.CommonUtils;
import com.hbm.usercenter.utils.LogUtils;
import com.hbm.usercenter.utils.UIUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Map;

public abstract class BaseFragmentRequest extends BaseFragmentCommon {
    @Override
    protected void doLogin() {
        super.doLogin();
        sendRequest();
    }
    Map<String, String> params;
    private void sendRequest() {
        params = getParams();
        NetClient.getNetClient().callNet(getURL(), new MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String json) {
                processResult(json);
                LogUtils.d(json);

            }
        },params);

    }




    private void processResult(String json) {
        JSONObject rootObject = null;
        try {
            rootObject = new JSONObject(json);
            final String err_msg = rootObject.getString("err_msg");
            String err_code = rootObject.getString("err_code");
            if (err_code.equals("-1")) {
                //                Intent intent = new Intent(getActivity(), QRCodeActivity.class);
                //                intent.putExtra("money", number);
                //                //                intent.putExtra("money", money);
                //                //                intent.putExtra("picUrl", picUrl);
                //                //
                //                startActivity(intent);
                doSuccess();
                //                startActivity(new Intent(getActivity(), MyUserInfoActivity.class));
            } else {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(),err_msg);
                    }
                });
                //                Toast.makeText(UIUtils.getContext(), err_msg, Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }



    protected abstract Map<String,String> getParams() ;
    protected abstract String getURL();
    protected abstract void doSuccess();
}
